package me.shurufa.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import me.shurufa.R;
import me.shurufa.holder.DiscoverBookViewHolder;
import me.shurufa.model.Book;

/**
 * 发现页面图书列表适配器
 *
 * @FileName: me.shurufa.adapter.DiscoverBookAdapter.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-21 18:58
 */
public class DiscoverBookAdapter extends RecyclerView.Adapter<DiscoverBookViewHolder>{

    List<Book> dataList;
    Object parent;

    public DiscoverBookAdapter(Object parent,List<Book> dataList){
        this.dataList = dataList;
        this.parent = parent;
    }

    @Override
    public DiscoverBookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_book, parent, false);
        DiscoverBookViewHolder holder = new DiscoverBookViewHolder(v,parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(DiscoverBookViewHolder holder, int position) {
        Book book = dataList.get(position);
        holder.bind(book);
    }

    public void append(Book[] items) {
        int pos = dataList.size();
        for (Book item : items) {
            if (!dataList.contains(item))
                dataList.add(item);
        }
        notifyItemRangeInserted(pos, items.length);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void remove(int position){
        if(dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear(){
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }

}
